package cn.lystudio.rohm_time_op.controller.webApi;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaMode;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.PageUtil;
import cn.lystudio.rohm_time_op.entity.dto.PlanOutputDTO;
import cn.lystudio.rohm_time_op.entity.po.OpItemPO;
import cn.lystudio.rohm_time_op.entity.po.PlanOutputPO;
import cn.lystudio.rohm_time_op.entity.vo.LayuiPageApiResponse;
import cn.lystudio.rohm_time_op.exceptions.JsonException;
import cn.lystudio.rohm_time_op.service.PlanOutputService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@SaCheckLogin
@RequestMapping("/webapi/planOutput")
@Slf4j
public class PlanOutputController {
    private PlanOutputService planOutputService;

    @Lazy
    @Autowired
    public void setPlanOutputService(PlanOutputService planOutputService) {
        this.planOutputService = planOutputService;
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @GetMapping("/findAll")
    public LayuiPageApiResponse findAll(Integer page, Integer limit, @RequestParam(name = "keyword", defaultValue = "") String keyword) {
        List<String> roleList = StpUtil.getRoleList(StpUtil.getLoginIdAsString());
        if (roleList.size() != 1) throw new JsonException(500, "权限异常");
        PageUtil.setFirstPageNo(1);
        List<PlanOutputDTO> all = planOutputService.getAll(keyword);
        if(CollUtil.isEmpty(all)) return LayuiPageApiResponse.ofSuccessData(null,0);
        return LayuiPageApiResponse.ofSuccessData(CollUtil.page(page,limit,all),all.size());
    }
    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/uptEnabled")
    public SaResult uptEnabled(Integer planOutputID, Boolean enabled) {
        return SaResult.data(planOutputService.uptPlanOutputEnabled(planOutputID,enabled));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/delPlanOutput")
    public SaResult delPlanOutput(Integer planOutputID){
        return SaResult.data(planOutputService.delPlanOutput(planOutputID));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/addPlanOutput")
    public SaResult addPlanOutput(@RequestBody PlanOutputPO planOutputPO ){
        return  SaResult.data(planOutputService.addPlanOutput(planOutputPO));
    }

    @SaCheckRole(value = {"1", "11"}, mode = SaMode.OR)
    @PostMapping("/uptPlanOutput")
    public SaResult uptPlanOutput(@RequestBody PlanOutputPO planOutputPO){
        return  SaResult.data(planOutputService.uptPlanOutput(planOutputPO));

    }


}
